Code as Action is a powerful concept in BRX that allows you to use BRKs to generate and execute code, enabling your AI applications to perform real-world actions and integrate with external systems.
The simplest implementation of Code as Action is direct code generation:
Copy
Ask AI
const codeGenerationBrk = { brxId: 'code-generation-brk', brxName: 'Code Generation', description: 'Generates code based on a natural language request', prompt: { prompt: new Map([ ['main', ` Generate JavaScript code to accomplish the following task: {{task_description}} The code should be executable in a Node.js environment. Only include the code, no explanations or comments. `] ]) }, processParams: { processType: 0 }, dependantBrxIds: new Map([ ['main_brx_entry_schema', 'code-generation-brk'] ])};
Code as Action can be used to integrate with external APIs:
Copy
Ask AI
// Generate API integration codeconst generateApiIntegration = async (apiDescription, task) => { const apiIntegrationBrkSchema = await brx.get('api-integration-brk'); const apiIntegrationBrk = new BRK(apiIntegrationBrkSchema); apiIntegrationBrk.input['api_description'] = apiDescription; apiIntegrationBrk.input['task'] = task; const result = await brx.run(apiIntegrationBrk); return result[0].brxRes.output;};// Example usageconst apiCode = await generateApiIntegration( `The Weather API is available at https://api.weather.com/v1/current It requires an API key passed as a query parameter 'key' It returns JSON with the current weather for a given location`, 'Get the current weather for New York');// Execute the generated API integration code// (in a production environment, with proper security measures)
Code as Action enables automation of repetitive tasks:
Copy
Ask AI
// Generate automation scriptconst generateAutomationScript = async (taskDescription) => { const automationBrkSchema = await brx.get('automation-brk'); const automationBrk = new BRK(automationBrkSchema); automationBrk.input['task_description'] = taskDescription; const result = await brx.run(automationBrk); return result[0].brxRes.output;};// Example usageconst automationScript = await generateAutomationScript( 'Create a script that monitors a folder for new CSV files, processes them to calculate averages for each column, and saves the results to a summary file');// Save the automation script to a filefs.writeFileSync('automation.js', automationScript);// Execute the automation script// (in a production environment, with proper security measures)
Design your prompts to generate safe, efficient code:
Copy
Ask AI
Generate JavaScript code to accomplish the following task:{{task_description}}Requirements:1. The code should be efficient and use modern JavaScript features2. Do not use any dangerous functions like eval, exec, or Function constructor3. Handle errors gracefully4. Include appropriate input validation5. Only use the following allowed modules: {{allowed_modules}}Only include the code, no explanations or comments.
Code as Action is a powerful paradigm that enables BRX to bridge the gap between natural language and executable code. By following the patterns and best practices outlined in this guide, you can create AI applications that not only understand user requests but can also take concrete actions to fulfill them.When implementing Code as Action, always prioritize security and robustness. Generated code should be thoroughly validated, tested, and executed in a controlled environment to prevent unintended consequences.With the right approach, Code as Action can unlock new possibilities for AI-powered automation, integration, and problem-solving.